﻿using System;
using System.IO;
using Microsoft.WindowsAzure.Storage;

namespace Altairis.FluffyCloud.Storage {

    public static class UserIcon {

        public static bool Delete(string userName) {
            if (userName == null) throw new ArgumentNullException("userName");
            if (string.IsNullOrWhiteSpace(userName)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "userName");
            userName = userName.ToLower().Trim();

            var blob = BlobHelper.UserIconContainer.GetBlockBlobReference(userName + ".png");
            return blob.DeleteIfExists();
        }

        public static byte[] Download(string userName, int size) {
            if (userName == null) throw new ArgumentNullException("userName");
            if (string.IsNullOrWhiteSpace(userName)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "userName");
            if (size < 20 || size > 150) throw new ArgumentOutOfRangeException("size", "Value must be between 20 and 150.");
            userName = userName.ToLower().Trim();

            var blob = BlobHelper.UserIconContainer.GetBlockBlobReference(userName + ".png");
            try {
                using (var ms = new MemoryStream()) {
                    blob.DownloadToStream(ms);
                    if (size == 150) {
                        // No resampling necessary, just return it as is
                        return ms.ToArray();
                    }
                    else {

                        // Resample to desired size
                        using (var sourceImage = System.Drawing.Image.FromStream(ms))
                        using (var targetImage = sourceImage.ResizeImage(new System.Drawing.Size(size, size)))
                        using (var targetStream = new MemoryStream()) {
                            targetImage.Save(targetStream, System.Drawing.Imaging.ImageFormat.Png);
                            return targetStream.ToArray();
                        }
                    }

                }
            }
            catch (StorageException) {
                return null;
            }
        }

        public static bool Upload(string userName, Stream imageStream) {
            if (userName == null) throw new ArgumentNullException("userName");
            if (string.IsNullOrWhiteSpace(userName)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "userName");
            if (imageStream == null) throw new ArgumentNullException("imageData");
            userName = userName.ToLower().Trim();

            try {

                // Try to load stream as image
                using (var sourceImage = System.Drawing.Image.FromStream(imageStream)) {
                    if (sourceImage.Width < 150 || sourceImage.Height < 150) return false; // too small

                    // Resize to 150x150
                    using (var targetImage = sourceImage.ResizeAndCrop(new System.Drawing.Size(150, 150))) {

                        // Save to blob storage
                        using (var ms = new MemoryStream()) {
                            targetImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                            ms.Seek(0, SeekOrigin.Begin);
                            var blob = BlobHelper.UserIconContainer.GetBlockBlobReference(userName + ".png");
                            blob.Properties.ContentType = "image/png";
                            blob.UploadFromStream(ms);
                        }
                    }
                }
            }
            catch (Exception) {
                return false;
            }

            return true;
        }

        public static bool Verify(string userName) {
            if (userName == null) throw new ArgumentNullException("userName");
            if (string.IsNullOrWhiteSpace(userName)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "userName");
            userName = userName.ToLower().Trim();

            var blob = BlobHelper.UserIconContainer.GetBlockBlobReference(userName + ".png");
            return blob.Exists();
        }
    }
}